package com.guoning.office_system.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.guoning.office_system.model.Meeting;
import com.guoning.office_system.model.User;
import com.guoning.office_system.service.MeetingService;
import com.guoning.office_system.utils.Constants;
import com.guoning.office_system.utils.JsonResult;
import com.guoning.office_system.vo.Criteria;
import com.guoning.office_system.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/meeting")
public class MeetingController extends BaseController {
    @Autowired
    MeetingService meetingService;

    @RequestMapping(value = "/getStateCount")
    public JsonResult getStateCount(HttpServletRequest request){
        JsonResult jsonResult;
        try {
            User user = getUserDate(request);
            Criteria criteria = new Criteria();
            criteria.addExtField("userId", user.getId());
            Map map = meetingService.getTabsCount(criteria);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "获取标签成功", map);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "获取标签异常");
        }
        return jsonResult;
    }

    /**
     * 查询用户会议列表
     * @param request
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/selectByUser")
    public JsonResult seleceByUser(HttpServletRequest request,
                                   @RequestParam(value = "pageIndex", defaultValue = "0") Integer pageIndex,
                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        JsonResult jsonResult;
        try {
            User user = getUserDate(request);
            PageBean pageBean = new PageBean(pageIndex, pageSize);
            Criteria criteria = new Criteria();
            criteria.addExtField("userId", user.getId());
            pageBean = meetingService.selectByUser(criteria, pageBean);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "查询成功", pageBean);
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "查询异常");
        }
        return jsonResult;
    }

    /**
     * 查询会议参会人员（分页）
     * @param pageIndex
     * @param pageSize
     * @param recordId
     * @return
     */
    @RequestMapping(value = "/selectByRecord")
    public JsonResult selectByRecord(@RequestParam(value = "pageIndex", defaultValue = "0") Integer pageIndex,
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                     @RequestParam(value = "recordId", defaultValue = "") Long recordId){
        JsonResult jsonResult;
        try {
            PageBean pageBean = new PageBean(pageIndex, pageSize);
            Criteria criteria = new Criteria();
            criteria.addExtField("recordId", recordId);
            pageBean = meetingService.selectByRecord(criteria, pageBean);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "查询成功", pageBean);
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "查询异常");
        }
        return jsonResult;
    }

    /**
     * 查询会议参会人员
     * @param recordId
     * @return
     */
    @RequestMapping(value = "/selectByRecordAllUser")
    public JsonResult selectByRecord(@RequestParam(value = "recordId", defaultValue = "") Long recordId){
        JsonResult jsonResult;
        try {
            Criteria criteria = new Criteria();
            criteria.addExtField("recordId", recordId);
            List list = meetingService.selectByRecordAllUser(criteria);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "查询成功", list);
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "查询异常");
        }
        return jsonResult;
    }

    /**
     * 删除
     * @param params
     * @return
     */
    @RequestMapping(value = "/deleteByRecordId", method = RequestMethod.POST)
    public JsonResult deleteByRecordId(String params){
        JsonResult jsonResult;
        try {
            List<Long> recordIds = JSONArray.parseArray(params, Long.class);
            int i = meetingService.deleteByRecordId(recordIds);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "删除成功");
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "删除异常");
        }
        return jsonResult;
    }

    /**
     * 删除
     * @param params
     * @return
     */
    @RequestMapping(value = "/deleteByUserIdAndRecordId", method = RequestMethod.POST)
    public JsonResult deleteByUserIdAndRecordId(String params){
        JsonResult jsonResult;
        try {
            Map param = JSONObject.parseObject(params, Map.class);
            Long recordId = JSONObject.parseObject(param.get("recordId").toString(), Long.class);
            List<Long> list = JSONArray.parseArray(param.get("list").toString(), Long.class);
            Criteria criteria = new Criteria();
            criteria.addExtField("recordId", recordId);
            criteria.addExtField("list", list);
            int i = meetingService.deleteByUserIdAndRecordId(criteria);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "操作成功");
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "操作异常");
        }
        return jsonResult;
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JsonResult insert(HttpServletRequest request, String params){
        JsonResult jsonResult;
        try {
            User user = getUserDate(request);
            List<Meeting> list = JSONArray.parseArray(params, Meeting.class);
            for (int i=0; i<list.size(); i++){
                list.get(i).setCreator(user.getUserName());
            }
            int i = meetingService.insert(list);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "操作成功");
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "操作异常");
        }
        return jsonResult;
    }
}
